package com.vue.vuedemo.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.vue.vuedemo.pojo.User;
import com.vue.vuedemo.service.UserService;
import com.vue.vuedemo.util.JwtUtil;
import com.vue.vuedemo.viewpojo.ResObject;

@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	@PostMapping("/login1")
	public ResObject<String> login(HttpServletResponse response,@RequestParam("username")String username,@RequestParam("password")String password) {
		 response.setHeader("Access-Control-Allow-Origin", "*");
		User userInfo = userService.getUserByUsername(username,password);
		if(userInfo != null) {
			String token = JwtUtil.createJWT(7000000,userInfo);
			ResObject<String> res = new ResObject();
		    res.setStatus(0);
		    res.setMessage("success");
		    res.setToken(token);
		    
		    return res;
		}else {
			ResObject<String> res = new ResObject();
		    res.setStatus(-1);
		    res.setMessage("error");
			return res;
		}
	}
	
	
	
}
